package eu.smoothit.sis.admin.backendBean;

import java.util.List;

import eu.smoothit.sis.db.SisDAOFactory;
import eu.smoothit.sis.db.api.daos.IPeerStatusDAO;
import eu.smoothit.sis.db.api.daos.ISwarmDAO;
import eu.smoothit.sis.db.impl.entities.PeerStatusEntry;
import eu.smoothit.sis.db.impl.entities.SwarmEntityEntry;

import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;

public class SwarmEntityStatistics {
	private List<PeerStatusEntry> associatedIopList=new ArrayList<PeerStatusEntry>(); // show sum of number in
	// overview
	private List<PeerStatusEntry> associatedLocalPeerList=new ArrayList<PeerStatusEntry>(); // show sum of number
	private SwarmEntityEntry swarm;
	private Integer nrOfAssociatedIop;
	private Integer nrOfAssociatedLocalPeers;
	private Float downloadRatePerSwarm=0F;

	// getter and setter
	// --------------------------------------------------------------------------------
	public Float getDownloadRatePerSwarm() {
		return downloadRatePerSwarm;
	}

	public Integer getNrOfAssociatedIop() {
		return nrOfAssociatedIop;
	}

	public void setNrOfAssociatedIop(Integer nrOfAssociatedIop) {
		this.nrOfAssociatedIop = nrOfAssociatedIop;
	}

	public Integer getNrOfAssociatedLocalPeers() {
		return nrOfAssociatedLocalPeers;
	}

	public void setNrOfAssociatedLocalPeers(Integer nrOfAssociatedLocalPeers) {
		this.nrOfAssociatedLocalPeers = nrOfAssociatedLocalPeers;
	}
	
	public SwarmEntityEntry getSwarm() {
		return swarm;
	}
	
	public void setSwarm(SwarmEntityEntry swarm) {
		this.swarm = swarm;
	}
	public List<PeerStatusEntry> getAssociatedIopList() {
//		boolean iopFlag=true;
//		associatedIopList=fetchAssociatedIopOrLocalPeerList(iopFlag);
		return associatedIopList;
	}


	public List<PeerStatusEntry> getAssociatedLocalPeerList() {
//		boolean iopFlag=false;
//		associatedLocalPeerList=fetchAssociatedIopOrLocalPeerList(iopFlag);
		return associatedLocalPeerList;
	}

//	public Map<SwarmEntityEntry, List<PeerStatusEntry>> getAssociatedLocalPeerAndIoPList() {
//		associatedLocalPeerAndIoPList=fetchAssociatedIopAndLocalPeerList();
//		return associatedLocalPeerAndIoPList;
//	}

	// do counting of IoP or local peers for all swarms
	// return top
//	public List<PeerStatusEntry>fetchAssociatedIopOrLocalPeerList(
//			boolean iopFlag) {
//		List<PeerStatusEntry> searchResult = null;
//		SisDAOFactory factory = SisDAOFactory.getFactory();
////		ISwarmDAO swarm_dao = factory.createSwarmDAO();
//		// TODO swarmEntity.getTorrentHash=PeerStatusEntry.filename?
//		// List<String>swarms_fileList=new ArrayList<String>();
////		List<SwarmEntityEntry> swarmEntities = swarm_dao.getAll();
////		if (!swarmEntities.isEmpty()) {
////			for (SwarmEntityEntry swarm : swarmEntities) {
//				IPeerStatusDAO peer_status_dao = factory.createPeerStatusDAO();
//				PeerStatusEntry ps = new PeerStatusEntry();
//				// search a specific swarm associated peers/iop based on its
//				// filename
//				ps.setFilename(swarm.getTorrentHash());
//				if (iopFlag)
//					ps.setIop_flag(true);
//				searchResult = peer_status_dao.get(ps);
////			}
////		}
//		return searchResult;
//	}
	
	public void statistics(
			) {
		List<PeerStatusEntry> searchResult = null;
		downloadRatePerSwarm=0F;
		SisDAOFactory factory = SisDAOFactory.getFactory();
				IPeerStatusDAO peer_status_dao = factory.createPeerStatusDAO();
				PeerStatusEntry ps = new PeerStatusEntry();
				// search a specific swarm associated peers/iop based on its
				// filename
				ps.setFilename(swarm.getTorrentHash());
				searchResult = peer_status_dao.get(ps);
				if(!searchResult.isEmpty()){
					associatedIopList.clear();
					associatedLocalPeerList.clear();
					for(PeerStatusEntry psn:searchResult){
						if(psn.getIop_flag()!=null){
							
							if(psn.getIop_flag()==true){
								associatedIopList.add(psn);
							}
							else 
								associatedLocalPeerList.add(psn);
						}	
					}
					nrOfAssociatedIop=associatedIopList.size();
					nrOfAssociatedLocalPeers=associatedLocalPeerList.size();
					
					for(PeerStatusEntry pst:associatedLocalPeerList){
						if(pst.getDown_rate()!=null)
						downloadRatePerSwarm=downloadRatePerSwarm+pst.getDown_rate();
					}
					
				}
		
	}
	

	
//	public static void main(String args[]) {
//		SisDAOFactory factory = SisDAOFactory.getFactory();
//		ISwarmDAO swarm_dao = factory.createSwarmDAO();
//		SwarmEntityStatistics ins=new SwarmEntityStatistics();
//		List<SwarmEntityEntry> swarmEntities = swarm_dao.getAll();
//		if (!swarmEntities.isEmpty()) {
//			for (SwarmEntityEntry sw : swarmEntities) {
//				ins.setSwarm(sw);
//				ins.statistics();
//				ins.getNrOfAssociatedIop();
//				ins.getNrOfAssociatedLocalPeers();
//			}
//			}
//		
//	}
	
}
